package br.com.maps.contacts.database;

import android.content.Context;

public class DBHandler extends ContactsDAO {
	private static final String[] SQL_DATABASE_DELETE = { "DROP TABLE IF EXISTS " + NOME_TABELA_CONTATOS,
			"DROP TABLE IF EXISTS " + NOME_TABELA_ASSOC, "DROP TABLE IF EXISTS " + NOME_TABELA_RAMAIS };

	private static final String[] SQL_DATABASE_CREATE = {
			"CREATE TABLE assoc_contatos_ramais(_id INTEGER PRIMARY KEY autoincrement, contact_id INTEGER not NULL, ramal_id INTEGER NOT NULL)",
			"CREATE TABLE contatos(_id integer primary key autoincrement, nome VARCHAR(60) not null, email VARCHAR(30), favorito INTEGER not null DEFAULT 0)",
			"CREATE TABLE ramais(_id INTEGER PRIMARY KEY autoincrement, value VARCHAR(4) not NULL)" };

	private SQLiteHelper sqliteHelper;

	public DBHandler(Context context, int dbVersion) {
		this.sqliteHelper = new SQLiteHelper(context, NOME_BANCO, dbVersion, SQL_DATABASE_CREATE, SQL_DATABASE_DELETE);
		this.db = this.sqliteHelper.getWritableDatabase();
	}

	@Override
	public void close() {
		super.close();
		if (this.sqliteHelper != null) {
			this.sqliteHelper.close();
		}
	}
}
